import React, { useState } from 'react';
import { Input, Button, Space, Table, Tag } from 'antd';
import AddPersonModal from '../../components/AddPersonModal'

// 引入css
import common from '../css/Common.module.scss'


const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
    key: 'name',
    render: (text) => <a>{text}</a>,
  },
  {
    title: 'Age',
    dataIndex: 'age',
    key: 'age',
  },
  {
    title: 'Address',
    dataIndex: 'address',
    key: 'address',
  },
  {
    title: 'Tags',
    key: 'tags',
    dataIndex: 'tags',
    render: (_, { tags }) => (
      <>
        {tags.map((tag) => {
          let color = tag.length > 5 ? 'geekblue' : 'green';
          if (tag === 'loser') {
            color = 'volcano';
          }
          return (
            <Tag color={color} key={tag}>
              {tag.toUpperCase()}
            </Tag>
          );
        })}
      </>
    ),
  },
  {
    title: 'Action',
    key: 'action',
    render: (_, record) => (
      <Space size="middle">
        <a>编辑</a>
        <a>删除</a>
      </Space>
    ),
  },
];

const data = [
  {
    key: '1',
    name: 'John Brown',
    age: 32,
    address: 'New York No. 1 Lake Park',
    tags: ['nice', 'developer'],
  },
  {
    key: '2',
    name: 'Jim Green',
    age: 42,
    address: 'London No. 1 Lake Park',
    tags: ['loser'],
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 32,
    address: 'Sydney No. 1 Lake Park',
    tags: ['cool', 'teacher'],
  },
];
const { Search } = Input;
const onSearch = (value, _e, info) => console.log(info?.source, value);

function Person() {
  const [modalIsOpen, setModalIsOpen] = useState(false);
  const openModal = () => {
    setModalIsOpen(true);
  }
  const closeModal = () => setModalIsOpen(false);

  const submitForm = (form) => {
    console.log(form, 'sss')
    form.validateFields()
    // setTimeout(function() {
    //   console.log("这是一个单次计时器");
    //     setModalIsOpen(false);
    // }, 3000);
  }

  return <div>
      <div className={`${common.space_between} ${common.mb16}`}>
        <Button type="primary" onClick={() => openModal()}>新增</Button>
        <Search placeholder="请输入" allowClear onSearch={onSearch} style={{ width: 200 }} />
      </div>
      <Table columns={columns} dataSource={data} />
      <AddPersonModal isOpen={modalIsOpen} onClose={closeModal} handleOk={submitForm} />
    </div>
}

export default Person
